import { login,phoneLogin, logout, getInfo, editPhone,reqAssistRoleBtnList} from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    user:{},
    phone:''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },

    SET_USER:(state, user) => {
      state.user = user
    },
    SET_PHONE:(state, phone) => {
      state.phone = phone
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      // const code = userInfo.code
      // const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login({
          phone:username,
          password:password
        }).then(res => {
          setToken(res.data)
          commit('SET_TOKEN', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登录
    PhoneLogin({ commit }, userInfo) {
      const phone = userInfo.phone.trim()
      const code = userInfo.code
      return new Promise((resolve, reject) => {
        phoneLogin({ phone, code }).then(res => {
          setToken(res.data)
          commit('SET_TOKEN', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    getAssistRoleBtnList({commit}, state) {
      return new Promise((resolve, reject) => {
        reqAssistRoleBtnList().then(res => {
          commit('SET_PERMISSIONS', res.data);
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.data
          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : this.domain + user.avatar;
          commit('SET_NAME', user.user_login)
          commit('SET_AVATAR', avatar)
          commit('SET_USER', user)
          commit('SET_PHONE', user.mobile)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // // 更新用户信息
    // UpdataInfo({commit, state },) {
    //   return new Promise((resolve, reject) => {

    //   })
    // },

    // UploadAvatar({ commit },avatar){
    //   return new Promise((resolve, reject) => {
    //     editPhoto({photo:avatar}).then(() => {
    //       commit('SET_AVATAR',process.env.VUE_APP_BASE_API+'/'+avatar)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    EditPhone({ commit },data){
      return new Promise((resolve, reject) => {
        editPhone(data).then(() => {
          commit('SET_PHONE',data.phone)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_NAME', '')
          commit('SET_USER', {})
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_NAME', '')
        commit('SET_USER', {})
        removeToken()
        resolve()
      })
    }
  }
}

export default user
